package com.stx.dao.impl;

import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.stx.dao.StudentDao;
import com.stx.dao.impl.TeacherDaoImpl.info;
import com.stx.domain.Absent;
import com.stx.domain.Admin;
import com.stx.domain.Building;
import com.stx.domain.Dorm;
import com.stx.domain.Student;
import com.stx.domain.Teacher;

@Repository
public class StudentDaoImpl implements StudentDao {
	/*@Resource
	private Student st;*/
	@Resource
	private JdbcTemplate jt;

	public List<Student> selectStudent(String building) {
		String sql = "select * from s where b='"+building+"'";
		List<Student> list = jt.query(sql, new rowMapper());
		return list;
	}
	
	@Override
	public Student selectStudent(int no) {
		// TODO Auto-generated method stub
		String sql = "select * from s where no='"+no+"'";
		List<Student> list = jt.query(sql, new rowMapper());
		if(list.size()==1)
			return  list.get(0);
		return null;
	}
	
	@Override
	public List<Student> selectStudent() {
		// TODO Auto-generated method stub
		String sql = "select * from s";
		List<Student> list = jt.query(sql, new rowMapper());
		return list;
	}
	
	public Student studentLogin(int no,String pwd){
		String sql="select * from s where no='"+no+"' and pwd='"+pwd+"'";
		List<Student> list=jt.query(sql, new rowMapper());
		if(list.size()==1)
			return list.get(0);
		return null;
	}

	public void updateStudent(Student student) {
		String sql = "update s set name=? , major=? where no=?";
		Object[] obj = { student.getName(), student.getMajor(), student.getNo() };
		jt.update(sql, obj);
	}

	public void studentDelete(int no) {
		String sql = "delete from s where no=" + no + "";
		jt.update(sql);
	}

	public List<Student> selectLived(String name) {
		String sql = "select * from s where d=?";
		Object[] obj = { name };
		List<Student> list = jt.query(sql, obj, new rowMapper());
		return list;

	}

	public void updateDorm(Student student) {

		String sql = "update s set b=?,d=? where no=?";
		Object[] obj = { student.getB(), student.getD(), student.getNo() };
		jt.update(sql, obj);
	}
	
	public int modifyStudentPwd(int no,String newpassword){	
		return jt.update("update s set pwd='"+newpassword+"' where no="+no);
	}
	
	@SuppressWarnings("rawtypes")
	class rowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int arg1) throws SQLException {
			Student student = new Student();
			student.setId(rs.getInt("id"));
			student.setNo(rs.getInt("no"));
			student.setName(rs.getString("name"));
			student.setPwd(rs.getString("pwd"));
			student.setSex(rs.getString("sex"));
			student.setMajor(rs.getString("major"));
			student.setD(rs.getString("d"));
			student.setB(rs.getString("b"));
			return student;
		}
	}

	@Override //用程序多次执行添加学生的操作做，然后plsql导出到excel表删除ID列，然后就可用于导入文件
	public void insertStudentForBatch(List<Object> studentList) {
		String sql = "insert into s values(id.nextval,?,?,123,?,?,?,?)";
		List<Object[]> batchArgs=new ArrayList<Object[]>();
		for(int i=0;i<studentList.size();i++){
			List listOut=(List) studentList.get(i);
			
			Object[] objectArray=new Object[6];
			objectArray[0]=Integer.parseInt(listOut.get(0).toString());
			objectArray[1]=listOut.get(1);
			objectArray[2]=listOut.get(2);
			objectArray[3]=listOut.get(3);
			objectArray[4]=listOut.get(4);
			objectArray[5]=listOut.get(5);
			batchArgs.add(objectArray);
		}
	
		/*List<Object[]> batchArgs=new ArrayList<Object[]>();
		Iterator<Object> it=studentList.iterator();
		while(it.hasNext()){
			System.out.println(it.next().toString());
			System.out.println(it.next().getClass());
			it.next();
			Object[] objectArray=new Object[6];
			objectArray[0]=student.getNo();
			objectArray[1]=student.getName();
			objectArray[2]=student.getSex();
			objectArray[3]=student.getMajor();
			objectArray[4]=student.getB();
			objectArray[5]=student.getD();
			batchArgs.add(objectArray);
		}*/
		
		jt.batchUpdate(sql, batchArgs);
		System.out.println("总人数" + studentList.size());
		//或者用下面一句，灵感来自源码1110行上下。
		//jt.batchUpdate(sql, batchArgs, new int[0]);
	}

	

	
}
